global numBins

[FileName PathName] = uigetfile('*.jpg','Select a JPG Image.');
colorSpace = input('Run in which color space? [hsi, hsv, rgb]: ', 's');
if isempty(colorSpace)
    colorSpace = 'hsi';
end
distanceComparison = input('Choose a distance comparsion. [manhattan, cosine, intersection]: ', 's');
if isempty(distanceComparison)
    distanceComparison = 'manhattan';
end

classLabels = ['A' 'B' 'C' 'D' 'F' 'G' 'H' 'K' 'M' 'O' 'P' 'S' 'Z']; % note: currently do not use W - water bottle
C = numel(classLabels);
similarity = 'weightedComponents';

numBins = 100;

% Build the training models
tic;
load(['models_' colorSpace])


% Make a mask of true classes for the test images
list = dir([PathName '*.jpg']); list = {list(:).name};
T = numel(list); % T = number of test images

% Classify each test image
scores = zeros(C,T);
tic;
for t=1:T
    I = imread([testImgsPath list{t}]);
    
    % Get mask(s) from segmentation results
    ROIs = segmentation(I);
    
    ROIscores = zeros(size(ROIs,3),1);
    D = zeros(size(models,3),5);
    sumD = zeros(size(models,3),1);
    for r=1:size(ROIs,3)
        mask = ROIs(:,:,r);
        [chist1,chist2,chist3] = Color_Features(I,mask,colorSpace,true);
        [thist1,thist2] = Texture_Features(mask,I,1);
        
        % ----------------------------------------------------------------
        % Calculate similarity of test histogram(s) with the model
        % histogram(s) from each class and assign to class with highest
        % similarity
        vector = horzcat(chist1,chist2,chist3,thist1,thist2);
        P = reshape(permute(models,[1 3 2]),[],size(models,2));
        for f = 1:size(models,3)
            if strcmp(distanceComparison,'manhattan')
                D(f,1) = pdist2(chist1,P(f,:),'cityblock');
                D(f,2) = pdist2(chist2,P(f+1,:),'cityblock');
                D(f,3) = pdist2(chist3,P(f+2,:),'cityblock');
                D(f,4) = pdist2(thist1,P(f+3,:),'cityblock');
                D(f,5) = pdist2(thist2,P(f+4,:),'cityblock');
            elseif strcmp(distanceComparison,'cosine')
                D(f,1) = pdist2(chist1,P(f,:),'cosine');
                D(f,2) = pdist2(chist2,P(f+1,:),'cosine');
                D(f,3) = pdist2(chist3,P(f+2,:),'cosine');
                D(f,4) = pdist2(thist1,P(f+3,:),'cosine');
                D(f,5) = pdist2(thist2,P(f+4,:),'cosine');
            elseif strcmp(distanceComparison,'intersection')
                D(f,1) = 1-sum(min(chist1,P(f,:)));
                D(f,2) = 1-sum(min(chist2,P(f+1,:)));
                D(f,3) = 1-sum(min(chist3,P(f+2,:)));
                D(f,4) = 1-sum(min(thist1,P(f+3,:)));
                D(f,5) = 1-sum(min(thist2,P(f+4,:)));
            end
            
            sumD = sum(D(:),2);
        end
        % Need to code compute scores using horzcat and save them!
        % ----------------------------------------------------------------
    end
    
end
time = toc;

% Calculate accuracy
[~, classes] = sort(scores,'descend');

% Find misclassified images